package com.retoc.oawx.controller;

import com.retoc.oawx.common.util.R;
import com.retoc.oawx.domain.Dept;
import com.retoc.oawx.domain.req.DeleteDeptReq;
import com.retoc.oawx.domain.req.InsertDeptReq;
import com.retoc.oawx.domain.req.UpdateDeptByIdReq;
import com.retoc.oawx.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api("部门模块网络接口")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/searchAllDept")
    @ApiOperation("查询所有部门数据")
    public R searchAllDept() {
        List<Dept> list = deptService.searchAllDept();
        return R.ok().put("result", list);
    }

    @PostMapping("/insertDept")
    @ApiOperation("添加部门")
    @RequiresPermissions(value = {"ROOT", "DEPT:INSERT"}, logical = Logical.OR)
    public R insertDept(@Valid @RequestBody InsertDeptReq req) {
        deptService.insertDept(req.getDeptName());
        return R.ok().put("result", "success");
    }

    @PostMapping("/deleteDeptById")
    @ApiOperation("删除部门")
    @RequiresPermissions(value = {"ROOT", "DEPT:DELETE"}, logical = Logical.OR)
    public R deleteDeptById(@Valid @RequestBody DeleteDeptReq req) {
        deptService.deleteDeptById(req.getId());
        return R.ok().put("result", "success");
    }

    @PostMapping("/updateDeptById")
    @ApiOperation("更新部门")
    @RequiresPermissions(value = {"ROOT", "DEPT:UPDATE"}, logical = Logical.OR)
    public R updateDeptById(@Valid @RequestBody UpdateDeptByIdReq req) {
        Dept entity = new Dept();
        entity.setId(req.getId());
        entity.setDeptName(req.getDeptName());
        deptService.updateDeptById(entity);
        return R.ok().put("result", "success");
    }
}

